//
//  LLRPScrollViewSegment.m
//  LoveLife
//
//  Created by xl on 2020/9/1.
//  Copyright © 2020 xl. All rights reserved.
//

#import "LLRPScrollViewSegment.h"
#import "NSString+XL.h"

@interface LLRPScrollViewSegment ()

@property (nonatomic, strong) XLScrollView *scrollView;
@property (nonatomic, strong) UIView * sliderView;
@property (nonatomic, strong) NSArray *titlesArr;
@property (nonatomic, strong) XLButton *selectBtn;

@end
@implementation LLRPScrollViewSegment

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.scrollView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.right.equalTo(self);
        }];
    }
    return self;
}

- (void)loadTitlesArr:(NSArray *)titlesArr {
    self.titlesArr = titlesArr;
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat margin_X = 20;
    CGFloat margin_W = 10;
    CGFloat slider_CX = 16;
    
    for (int i = 0 ; i < titlesArr.count ; i++) {
        NSString *title = titlesArr[i];
        CGFloat btn_W = [title calculateRowWidthWithFont:[UIFont fontOfPingFangSCSemiboldWithSize:17] height:25];
        XLButton *button = [[XLButton alloc] initWithFrame:CGRectMake(margin_X, 0, btn_W, self.height - 2)];
        button.tag = 1000+i;
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackFontColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redFontColor] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont fontOfPingFangSCRegularWithSize:14];
        [button addTarget:self action:@selector(tagBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        //默认第一个选中
        if (i == 0) {
            slider_CX = margin_X + btn_W/2.0;
            button.selected = YES;
            button.titleLabel.font = [UIFont fontOfPingFangSCSemiboldWithSize:17];
            self.selectBtn = button;
        }
        [self.scrollView addSubview:button];
        margin_X = CGRectGetMaxX(button.frame) + margin_W;
        if (i == titlesArr.count - 1) {
            CGFloat total_w = CGRectGetMaxX(button.frame) + 20;
            _scrollView.contentSize = CGSizeMake(total_w, self.height - 2);
        }
    }
    
    self.sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-2, 32, 2)];
    self.sliderView.centerX = slider_CX;
    self.sliderView.backgroundColor = [UIColor redColor];
    [self.scrollView addSubview:self.sliderView];
}

- (void)selectingOneTagWithIndex:(NSInteger )index {
    if (index == (self.selectBtn.tag - 1000)) {
        return;
    }
    XLButton *index_Btn = [_scrollView viewWithTag:(1000+index)];
    WEAKSELF;
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.sliderView.centerX = index_Btn.centerX;
    }];
    
    self.selectBtn.selected = NO;
    self.selectBtn.titleLabel.font = [UIFont fontOfPingFangSCRegularWithSize:14];

    index_Btn.selected = YES;
    index_Btn.titleLabel.font = [UIFont fontOfPingFangSCSemiboldWithSize:17];
    
    self.selectBtn = index_Btn;
    [_scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
}

- (void)tagBtnClick:(XLButton *)btn
{
    if (btn.tag == self.selectBtn.tag) {
        return;
    }
    WEAKSELF;
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.sliderView.centerX = btn.centerX;
    }];
    
    self.selectBtn.selected = NO;
    self.selectBtn.titleLabel.font = [UIFont fontOfPingFangSCRegularWithSize:14];

    btn.selected = YES;
    btn.titleLabel.font = [UIFont fontOfPingFangSCSemiboldWithSize:17];
    
    self.selectBtn = btn;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectedSegmentIndex:)]) {
        [self.delegate selectedSegmentIndex:btn.tag - 1000];
    }
}

#pragma mark - setter/getter
- (XLScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [XLScrollView new];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

@end
